package com.example.kingdee.KingdeeCreate.impl;

import com.example.kingdee.KingdeeCreate.CreateBill;
import com.example.kingdee.KingdeeCreate.dto.CreateInDto;
import com.example.kingdee.common.StringUtils;
import com.example.kingdee.exception.ProcedureException;
import com.example.kingdee.kingdeeBridge.common.RequestResult;
import com.example.kingdee.sqlTool.SqlTool;
import com.example.kingdee.sqlTool.common.DataObject;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 创建直接调拨单
 */
@Service
public class CreateCompanyAllotBillImpl implements CreateBill {

    @Autowired
    private SqlTool sqlTool;

    @Override
    public void preposition(CreateInDto createInDto) {

    }

    /**
     *
     * @param createInDto
     * reserved1为调入仓库编码，reserved2为调入仓位编码
     * @return
     */
    @Override
    public RequestResult createSrcBill(CreateInDto createInDto) {
        String workOrgId = "100";
        //单据类型 FBillTypeID
        Map<String, Object> fBillTypeID = new HashMap<>();
        fBillTypeID.put("FNUMBER", "ZJDB01_SYS");
        //调出组织
        Map<String, Object> srcOrgNumber = new HashMap<>();
        srcOrgNumber.put("FNumber", workOrgId);
        //调入组织
        Map<String, Object> inOrgNumber = new HashMap<>();
        inOrgNumber.put("FNumber", workOrgId);

        //单据明细集合
        List<Map<String, Object>> orderInfoList = new ArrayList<>();
        List<Map<String, Object>> dataOrderList = createInDto.getEntryList();
        for (Map<String, Object> dataRow : dataOrderList) {
            double qty = dataRow.get("qty") == null ? 0 : Double.parseDouble(dataRow.get("qty").toString());
            if (qty <= 0){
                continue;
            }
            Map<String, Object> materialId = new HashMap<>(); //物料编码
            materialId.put("FNumber", dataRow.get("materialNumber"));

            //调出仓库
            Map<String, Object> srcStockId = new HashMap<>();
            srcStockId.put("FNumber", dataRow.get("outStockNumber"));

            //调入仓库
            Map<String, Object> fDestStockID = new HashMap<>();
            fDestStockID.put("FNumber", createInDto.getReserved1());

            //明细实体
            Map<String,Object> orderEntry = new LinkedHashMap<>();
            orderEntry.put("FMaterialId", materialId);//物料编码
            orderEntry.put("FQty", qty);//调拨数量
            orderEntry.put("FSrcStockId", srcStockId);//调出仓库
            //调出仓位
            Map<String,Object> stockLocIdOut = new HashMap<>();
            Map<String,Object> stockLocKeyId = new HashMap<>();
            stockLocKeyId.put("FNumber", dataRow.get("outStockLocNumber"));
            if (dataRow.get("outStockLocNumber") != null){
                //获取仓位后缀
                String stockLocFlexNumber = sqlTool.getStockLocFlexNumber(dataRow.get("outStockLocNumber").toString(), dataRow.get("outStockNumber").toString());
                stockLocIdOut.put("FSRCSTOCKLOCID__" + stockLocFlexNumber, stockLocKeyId);
                orderEntry.put("FSrcStockLocId", stockLocIdOut);//调出仓位编码
            }
            orderEntry.put("FDestStockID", fDestStockID);//调入仓库编码
            if (!StringUtils.isEmpty(String.valueOf(createInDto.getReserved2()))){
                //调入仓位
                Map<String,Object> stockLocIdIn = new HashMap<>();
                Map<String,Object> stockLocKeyId2 = new HashMap<>(); //仓位
                stockLocKeyId2.put("FNumber", createInDto.getReserved2());
                //获取仓位后缀
                String stockLocFlexNumber2 = sqlTool.getStockLocFlexNumber(String.valueOf(createInDto.getReserved2()), String.valueOf(createInDto.getReserved1()));
                stockLocIdIn.put("FDESTSTOCKLOCID__" + stockLocFlexNumber2, stockLocKeyId2);
                orderEntry.put("FDestStockLocId", stockLocIdIn);//调入仓位编码
            }
            orderInfoList.add(orderEntry);
        }
        //添加分布式调出单
        DataObject dataObject = new DataObject(FormIdEnum.sTKTransferDirect);
        dataObject.setAttribute("FBillTypeID", fBillTypeID);//单据类型
        dataObject.setAttribute("FBizType", "NORMAL");//业务类型NORMAL——标准
        dataObject.setAttribute("FTransferDirect", "GENERAL");//调拨方向(必填项) RETURN——退货 GENERAL——普通
        dataObject.setAttribute("FTransferBizType", "InnerOrgTransfer");//调拨类型 InnerOrgTransfer——组织内调拨 OverOrgTransfer——跨组织调拨
        dataObject.setAttribute("FStockOutOrgId", srcOrgNumber);//调出库存组织
        dataObject.setAttribute("FOwnerTypeOutIdHead", "BD_OwnerOrg");//调出货主类型
        dataObject.setAttribute("FStockOrgId", inOrgNumber);//调入库存组织
        dataObject.setAttribute("FOwnerTypeIdHead", "BD_OwnerOrg");//调入货主类型
        dataObject.setAttribute("FDate", sqlTool.getNowForString());//日期
        dataObject.setAttribute("FBillEntry", orderInfoList);//明细

        try {
            Map<String, Object> result = sqlTool.editForDataObject(dataObject);
            if (result.get("code").toString().equals("200")){
                return RequestResult.ofSuccess(result);
            }else {
                return RequestResult.ofError(result.get("msg").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProcedureException("无法调用云星空接口");
        }
    }

    @Override
    public void auditTargetBill(String billNo) {

    }

    @Override
    public void postPosition(RequestResult srcBill, CreateInDto createInDto) {

    }

    @Override
    public void deleteTargetBill(String fid) {

    }
}
